﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;

namespace CheckFreeWayServer
{
    class Configuracao
    {
        private int _portaInicial;
        private int _portaFinal;

        public Configuracao()
        {
            try
            {
                RegistryKey Key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SinacorCheckFreeWayServer");
                string Path = Key.GetValue("ImagePath").ToString();
                Path = Path.Substring(1, Path.LastIndexOf('\\')) + "\\CheckFreeWay.config";
                using (StreamReader sr = new StreamReader(Path))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.IndexOf(':') > 0 && line.Split(':')[0] == "PortaInicial")
                        {
                            this._portaInicial = line.Split(':')[1].Length > 0 ? Convert.ToInt32(line.Split(':')[1]) : 0;
                        }
                        else if (line.IndexOf(':') > 0 && line.Split(':')[0] == "PortaFinal")
                        {
                            this._portaFinal = line.Split(':')[1].Length > 0 ? Convert.ToInt32(line.Split(':')[1]) : 0;
                        }
                    }
                }
            }catch(Exception ex)
            {
                this._portaInicial = 0;
                this._portaFinal = 0;
                using (StreamWriter sw = new StreamWriter("C:\\CheckFreeWayServerError.txt"))
                {
                   sw.Write(ex.Message);
                }
            }
        }

        public int GetPortaInicial()
        {
            return this._portaInicial;
        }

        public int GetPortaFinal()
        {
            return this._portaFinal;
        }
    }
}
